package com.whfc.mach.manager.impl;

import com.whfc.common.util.DateUtil;
import com.whfc.mach.dao.AppMachWarnRecordMapper;
import com.whfc.mach.dto.warn.AppMachWarnCountDTO;
import com.whfc.mach.enums.AppMachWarnRuleType;
import com.whfc.mach.manager.MachWarnManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author : qzexing
 * @version : 1.0
 * @date : 2021-02-22 15:23
 */
@Service
public class MachWarnManagerImpl implements MachWarnManager {

    @Autowired
    private AppMachWarnRecordMapper appMachWarnRecordMapper;

    @Override
    public AppMachWarnCountDTO machWarnStatistics(Integer deptId, List<Integer> machIds) {
        AppMachWarnCountDTO warnCountDTO = new AppMachWarnCountDTO();
        Date endTime = new Date();
        Date startTime = DateUtil.addDays(endTime, -1);
        int speedWarnNum = 0;
        int idleWarnNum = 0;
        int workWarnNum = 0;
        int stopWarnNum = 0;
        int engineWarnNum = 0;
        int fenceWarnNum = 0;
        int deviceWarnNum = 0;
        int rotationWarnNum = 0;
        int forceWarnNum = 0;
        List<Integer> machWarnRuleTypes = appMachWarnRecordMapper.selectRuleTypeList(deptId, machIds, null, null, startTime, endTime);

        for (Integer ruleType : machWarnRuleTypes) {
            if (AppMachWarnRuleType.IDLE.value().equals(ruleType)) {
                idleWarnNum += 1;
            } else if (AppMachWarnRuleType.WORK.value().equals(ruleType)) {
                workWarnNum += 1;
            } else if (AppMachWarnRuleType.STOP.value().equals(ruleType)) {
                stopWarnNum += 1;
            } else if (AppMachWarnRuleType.SPEED.value().equals(ruleType)) {
                speedWarnNum += 1;
            } else if (AppMachWarnRuleType.ENGINE.value().equals(ruleType)) {
                engineWarnNum += 1;
            } else if (AppMachWarnRuleType.FENCE.value().equals(ruleType)) {
                fenceWarnNum += 1;
            } else if (AppMachWarnRuleType.DEVICE.value().equals(ruleType)) {
                deviceWarnNum += 1;
            } else if (AppMachWarnRuleType.ROTATION.value().equals(ruleType)) {
                rotationWarnNum += 1;
            } else if (AppMachWarnRuleType.FORCE.value().equals(ruleType)) {
                forceWarnNum += 1;
            }
        }
        warnCountDTO.setSpeedWarnNum(speedWarnNum);
        warnCountDTO.setStopWarnNum(stopWarnNum);
        warnCountDTO.setIdleWarnNum(idleWarnNum);
        warnCountDTO.setWorkWarnNum(workWarnNum);
        warnCountDTO.setEngineWarnNum(engineWarnNum);
        warnCountDTO.setFenceWarnNum(fenceWarnNum);
        warnCountDTO.setRotationWarnNum(rotationWarnNum);
        warnCountDTO.setDeviceWarnNum(deviceWarnNum);
        warnCountDTO.setForceWarnNum(forceWarnNum);
        warnCountDTO.setWarnNum(speedWarnNum + stopWarnNum + fenceWarnNum + idleWarnNum + workWarnNum + engineWarnNum + deviceWarnNum + rotationWarnNum + forceWarnNum);
        return warnCountDTO;
    }

}
